package cn.dyp.com.automaticcontrolserver.action;


import org.json.JSONException;
import org.json.JSONObject;

import cn.dyp.com.automaticcontrolserver.bean.Controller;
import cn.dyp.com.automaticcontrolserver.serial.SerialportService;
import cn.dyp.com.automaticcontrolserver.util.Util;

/**
 * @author zongbingwu 获取所有控制器状态的请求处理类
 */
public class GetControllerStatusAction extends BaseAction {
	// action name
	public static final String TAG = "getContorllerStatus";

	@Override
	protected String jasonPorcess(String param) {
		JSONObject jsonResponse = new JSONObject();
		try {
			// 获取水泵控制器的状态
			jsonResponse.put("WaterPump", SerialportService
					.getControllerStatusByName(Controller.WATER_PUMP_CONTORL));
			// 获取风扇控制器的状态
			jsonResponse.put("Blower", SerialportService
					.getControllerStatusByName(Controller.BLOWER_CONTORL));
			// 获取路灯控制器的状态
			jsonResponse.put("Roadlamp", SerialportService
					.getControllerStatusByName(Controller.ROADLAMP_CONTORL));
			// 获取蜂鸣器控制器的状态
			jsonResponse.put("Buzzer", SerialportService
					.getControllerStatusByName(Controller.BUZZER_CONTORL));
			jsonResponse.put("result", "ok");
			logMsg = "Get controller status";
			logObj = "All Controller";
			return jsonResponse.toString();
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected String soapPorcess(String param) {
		String soapBody = "";
		// 获取水泵控制器的状态
		soapBody += Util
				.getXmlElementStr(
						"WaterPump",
						SerialportService
								.getControllerStatusByName(Controller.WATER_PUMP_CONTORL)
								+ "");
		// 获取风扇控制器的状态
		soapBody += Util.getXmlElementStr(
				"Blower",
				SerialportService
						.getControllerStatusByName(Controller.BLOWER_CONTORL)
						+ "");
		// 获取路灯控制器的状态
		soapBody += Util.getXmlElementStr(
				"Roadlamp",
				SerialportService
						.getControllerStatusByName(Controller.ROADLAMP_CONTORL)
						+ "");
		// 获取蜂鸣器控制器的状态
		soapBody += Util.getXmlElementStr(
				"Buzzer",
				SerialportService
						.getControllerStatusByName(Controller.BUZZER_CONTORL)
						+ "");
		soapBody += Util.getXmlElementStr("result", "ok");
		logMsg = "Get controller status";
		logObj = "All Controller";
		return Util.getSoapXml(soapBody);
	}

}
